ConfigSelection = ConfigSelection or Class("ConfigSelection", EventDispatcher)
local idCreator = IdCreator.new()
---@diagnostic disable-next-line: duplicate-set-field
function ConfigSelection:ctor(args)
    EventDispatcher.ctor(self)
    self.id = args.id
    self.name = args.name
    self.desc = args.desc
    self._eIdCreator = IdCreator.new()
    self._elements = {}
    self._dic = {}
    if args.elements then
        for _, element in ipairs(args.elements) do
            self:loadElement(element)
        end
    end
end

function ConfigSelection:changeName(name)
    self.name = name
    self:emit(ConfigEvent.ChangeSelectionName, name)
end

function ConfigSelection:loadElement(data)
    self._eIdCreator:load(data.id)
    local element = ConfigSelectionElement.new(data)
    table.insert(self._elements, element)
    self._dic[element.id] = element
end

function ConfigSelection:newElement()
    self:loadElement({
        id = self._eIdCreator:create(),
        name = "元素".. self._eIdCreator:getCount(),
        desc = "空"
    })
    self:emit(ConfigEvent.NewSelectionElement)
end

function ConfigSelection:getElementsProfile()
    local str = self._elements[1].name
    for i = 2, #self._elements do
        local e = self._elements[i]
        str = str.."|"..e.name
    end
    return str
end

function ConfigSelection:getElements()
    return self._elements
end

function ConfigSelection:getElement(id)
    return self._dic[id]
end

function ConfigSelection:getSaveData()
    local data = {}
    data.id = self.id
    data.name = self.name
    data.desc = self.desc
    data.elements = {}
    for _, element in ipairs(self._elements) do
        table.insert(data.elements, element:getSaveData())
    end
    return data
end